package com.yuerlive.rainbow.module.main.presenter;

import com.yuerlive.rainbow.api.HomApi;
import com.yuerlive.rainbow.api.HomeService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.Banner;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.LiveData;
import com.yuerlive.rainbow.entity.LiveInfo;
import com.yuerlive.rainbow.entity.ObjectWrapper;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.module.mine.presenter.MyFansContract;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.BiFunction;

/**
 * Created by xiaoyi on 2017/5/26.
 */

public class HomePresenter extends BasePresenterImpl implements HomeContract.Presenter {
    HomeContract.View view;
    HomeService service;
    List<ObjectWrapper> mResults = new ArrayList<>();
    public static final int TYPE_BANNER = 1;
    public static final int TYPE_LIVE = 0;
    private int page = 1;

    public HomePresenter(HomeContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        service = HomApi.getInstance().homeService;
    }

    @Override
    public void getHotBanner() {
        view.showLoading();
        service.getHotBanner().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<Banner>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<Banner>> listBaseData) {
                        view.hideLoading();

                    }

                    @Override
                    protected void onFail(String message) {
                        view.showLiveData(mResults, page);
                    }
                });
    }

    @Override
    public void getHotLive() {
        //  view.showLoading();
        page++;
        Map<String, String> params = new HashMap<>();
        params.put("page", page + "");
        mResults.clear();
        service.getHotLive(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<LiveData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<LiveData> liveDataBaseData) {
                        view.hideLoading();
                        for (LiveInfo live : liveDataBaseData.getObject().getList()) {
                            ObjectWrapper liveWrapper = new ObjectWrapper(TYPE_LIVE, live);
                            mResults.add(liveWrapper);
                        }
                        view.showMoreData(mResults);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.showLiveData(mResults, page);
                    }
                });
    }

    @Override
    public void getData() {
        page = 1;
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        params.put("page", page + "");
        view.showLoading();
        mResults.clear();
        Observable.zip(service.getHotBanner(),
                service.getHotLive(params),
                new BiFunction<BaseData<List<Banner>>, BaseData<LiveData>, List<ObjectWrapper>>() {
                    @Override
                    public List<ObjectWrapper> apply(@NonNull BaseData<List<Banner>> listBaseData, @NonNull BaseData<LiveData> liveDataBaseData) throws Exception {
                        if (listBaseData.getObject() != null) {
                            ObjectWrapper banner = new ObjectWrapper(TYPE_BANNER, listBaseData.getObject());
                            mResults.add(banner);
                        }
                        List<LiveInfo> lives = liveDataBaseData.getObject().getList();
                        for (LiveInfo live : lives) {
                            ObjectWrapper liveWrapper = new ObjectWrapper(TYPE_LIVE, live);
                            mResults.add(liveWrapper);
                        }
                        return mResults;
                    }
                }).compose(RxHelper.toUI())
                .subscribe(new RxObserver<List<ObjectWrapper>>(this) {
                    @Override
                    protected void onSuccess(List<ObjectWrapper> results) {
                        view.hideLoading();
                        view.showFullData(results);
                    }

                    @Override
                    protected void onFail(String message) {
                    }
                });
    }

    @Override
    public void refresh() {
        getData();
    }

    @Override
    public void getFollowed(boolean isLoadMore) {
        if (isLoadMore) {
            page++;
        } else {
            page = 1;
        }
        L.e("FragmentFollowed", "page " + page);
        Map<String, String> params = new HashMap<>();
        params.put("page", page + "");
        service.getFollowed(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<LiveData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<LiveData> liveDataBaseData) {
                        view.hideLoading();
                        mResults.clear();
                        for (LiveInfo live : liveDataBaseData.getObject().getList()) {
                            ObjectWrapper liveWrapper = new ObjectWrapper(TYPE_LIVE, live);
                            mResults.add(liveWrapper);
                        }
                        view.showLiveData(mResults, page);
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                        mResults.clear();
                        view.showLiveData(mResults, page);
                    }
                });
    }

}